<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//require_once 'Log.php';
class MainController extends CI_Controller {

    private $main_log;
    protected $locale = 'english';

    public function __construct() {
        parent::__construct();
        $this->main_log = new TS_Log(get_class());
        $this->setLocale('english');
    }

    public function setLocale($locale) {
        return $this->locale = $locale;
    }

    public static function loadView($class, $viewname, $output) {
        $data['locale'] = $class->locale;
        $class->main_log->log('info', '---------- isset($data) : ' . isset($output));
        $class->main_log->log('info', '---------- gettype($data) : ' . gettype($output));
        if (isset($output) && gettype($output) == 'array') {
            foreach ($output as $key => $value) {
                $data[$key] = $value;
            }
        }
        return $class->load->view($viewname, $data);
    }

    public static function getGender($input) {
        if (isset($input) && gettype($input) == 'string') {
            if ($input == 'M') {
                return 'Male';
            } else {
                return 'Female';
            }
        }
    }

    public static function getUserStatus($input) {
        if (isset($input) && gettype($input) == 'string') {
            if ($input == 'A') {
                return 'Administrator';
            } else if ($input == 'U') {
                return 'User';
            } else if ($input == 'P') {
                return 'Approveder';
            } else if ($input == 'UE') {
                return 'User & Endorsee';
            }
        }
    }

    public static function getCountry($input) {
        if (isset($input) && gettype($input) == 'string') {
            if ($input == 'en') {
                return 'English';
            } else if ($input == 'th') {
                return 'Thailand';
            }
        }
    }

    public static function convertFormatDate($input, $locale) {

        $TH_Day = array("อาทิตย์", "จันทร์", "อังคาร", "พุธ", "พฤหัสบดี", "ศุกร์", "เสาร์");
        $TH_Month = array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฏาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");

        $nDay = date("w");
        $nMonth = date("n") - 1;

        $type = gettype($input);
        log_message('info', '--------------------- type : ' . $type);

        if (isset($locale) && $locale == 'th_TH') {
            switch ($type) {
                case 'date' : return date_format($input, 'j') . ' ' . $TH_Month[$nMonth] . ' ' . (date_format($input, 'Y') + 543);
                case 'string' : return date_format(date_create($input), 'j') . ' ' . $TH_Month[$nMonth] . ' ' . (date_format(date_create($input), 'Y') + 543);
            }
        } else {
            $split = explode('_', $locale);
            log_message('info', '--------------------- split[0] : ' . $split[0]);
            log_message('info', '--------------------- split[1] : ' . $split[1]);
            setlocale(LC_ALL, $split[0], $split[1]);
            switch ($type) {
                case 'date' : return date_format($input, 'j F Y');
                case 'string' : return date_format(date_create($input), 'j F Y');
            }
        }
    }

}
